/*
 * Copyright (C) 2008-2014 TrinityCore <http://www.trinitycore.org/>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program. If not, see <http://www.gnu.org/licenses/>.
 */

/* ScriptData
Name: disable_commandscript
%Complete: 100
Comment: All disable related commands
Category: commandscripts
EndScriptData */

#include "DisableMgr.h"
#include "AchievementMgr.h"
#include "Chat.h"
#include "Chat/Command/Commands/LegacyCommand.hpp"
#include "Language.h"
#include "ObjectMgr.h"
#include "OutdoorPvP.h"
#include "Player.h"
#include "ScriptMgr.h"
#include "SpellMgr.h"
#include "DBCStores.h"

namespace chat { namespace command { namespace handler {

    bool HandleAddDisables(ChatHandler* handler, char const* args, uint8 disableType)
    {
        char* entryStr = strtok((char*)args, " ");
        if (!entryStr || !atoi(entryStr))
            return false;

        char* flagsStr = strtok(NULL, " ");
        uint8 flags = flagsStr ? uint8(atoi(flagsStr)) : 0;

        char* commentStr = strtok(NULL, "");
        if (!commentStr)
            return false;

        std::string disableComment = commentStr;
        uint32 entry = uint32(atoi(entryStr));

        std::string disableTypeStr = "";

        switch (disableType)
        {
        case DISABLE_TYPE_SPELL:
        {
            if (!sSpellMgr->GetSpellInfo(entry))
            {
                handler->PSendSysMessage(LANG_COMMAND_NOSPELLFOUND);
                handler->SetSentErrorMessage(true);
                return false;
            }
            disableTypeStr = "spell";
            break;
        }
        case DISABLE_TYPE_QUEST:
        {
            if (!sObjectMgr->GetQuestTemplate(entry))
            {
                handler->PSendSysMessage(LANG_COMMAND_QUEST_NOTFOUND, entry);
                handler->SetSentErrorMessage(true);
                return false;
            }
            disableTypeStr = "quest";
            break;
        }
        case DISABLE_TYPE_MAP:
        {
            if (!sMapStore.LookupEntry(entry))
            {
                handler->PSendSysMessage(LANG_COMMAND_NOMAPFOUND);
                handler->SetSentErrorMessage(true);
                return false;
            }
            disableTypeStr = "map";
            break;
        }
        case DISABLE_TYPE_BATTLEGROUND:
        {
            if (!sBattlemasterListStore.LookupEntry(entry))
            {
                handler->PSendSysMessage(LANG_COMMAND_NO_BATTLEGROUND_FOUND);
                handler->SetSentErrorMessage(true);
                return false;
            }
            disableTypeStr = "battleground";
            break;
        }
        case DISABLE_TYPE_ACHIEVEMENT_CRITERIA:
        {
            if (!sAchievementMgr->GetAchievementCriteria(entry))
            {
                handler->PSendSysMessage(LANG_COMMAND_NO_ACHIEVEMENT_CRITERIA_FOUND);
                handler->SetSentErrorMessage(true);
                return false;
            }
            disableTypeStr = "achievement criteria";
            break;
        }
        case DISABLE_TYPE_OUTDOORPVP:
        {
            if (entry > MAX_OUTDOORPVP_TYPES)
            {
                handler->PSendSysMessage(LANG_COMMAND_NO_OUTDOOR_PVP_FORUND);
                handler->SetSentErrorMessage(true);
                return false;
            }
            disableTypeStr = "outdoorpvp";
            break;
        }
        case DISABLE_TYPE_VMAP:
        {
            if (!sMapStore.LookupEntry(entry))
            {
                handler->PSendSysMessage(LANG_COMMAND_NOMAPFOUND);
                handler->SetSentErrorMessage(true);
                return false;
            }
            disableTypeStr = "vmap";
            break;
        }
        case DISABLE_TYPE_MMAP:
        {
            if (!sMapStore.LookupEntry(entry))
            {
                handler->PSendSysMessage(LANG_COMMAND_NOMAPFOUND);
                handler->SetSentErrorMessage(true);
                return false;
            }
            disableTypeStr = "mmap";
            break;
        }
        default:
            break;
        }

        PreparedStatement* stmt = NULL;
        stmt = WorldDatabase.GetPreparedStatement(WORLD_SEL_DISABLES);
        stmt->setUInt32(0, entry);
        stmt->setUInt8(1, disableType);
        PreparedQueryResult result = WorldDatabase.Query(stmt);
        if (result)
        {
            handler->PSendSysMessage("This %s (Id: %u) is already disabled.", disableTypeStr.c_str(), entry);
            handler->SetSentErrorMessage(true);
            return false;
        }

        stmt = WorldDatabase.GetPreparedStatement(WORLD_INS_DISABLES);
        stmt->setUInt32(0, entry);
        stmt->setUInt8(1, disableType);
        stmt->setUInt16(2, flags);
        stmt->setString(3, disableComment);
        WorldDatabase.Execute(stmt);

        handler->PSendSysMessage("Add Disabled %s (Id: %u) for reason %s", disableTypeStr.c_str(), entry, disableComment.c_str());
        return true;
    }

    bool HandleAddDisableSpellCommand(ChatHandler* handler, char const* args)
    {
        if (!*args)
            return false;

        return HandleAddDisables(handler, args, DISABLE_TYPE_SPELL);
    }

    bool HandleAddDisableQuestCommand(ChatHandler* handler, char const* args)
    {
        if (!*args)
            return false;

        return HandleAddDisables(handler, args, DISABLE_TYPE_QUEST);
    }

    bool HandleAddDisableMapCommand(ChatHandler* handler, char const* args)
    {
        if (!*args)
            return false;

        return HandleAddDisables(handler, args, DISABLE_TYPE_MAP);
    }

    bool HandleAddDisableBattlegroundCommand(ChatHandler* handler, char const* args)
    {
        if (!*args)
            return false;

        return HandleAddDisables(handler, args, DISABLE_TYPE_BATTLEGROUND);
    }

    bool HandleAddDisableAchievementCriteriaCommand(ChatHandler* handler, char const* args)
    {
        if (!*args)
            return false;

        return HandleAddDisables(handler, args, DISABLE_TYPE_ACHIEVEMENT_CRITERIA);
    }

    bool HandleAddDisableOutdoorPvPCommand(ChatHandler* handler, char const* args)
    {
        if (!*args)
            return false;

        HandleAddDisables(handler, args, DISABLE_TYPE_OUTDOORPVP);
        return true;
    }

    bool HandleAddDisableVmapCommand(ChatHandler* handler, char const* args)
    {
        if (!*args)
            return false;

        return HandleAddDisables(handler, args, DISABLE_TYPE_VMAP);
    }

    bool HandleAddDisableMMapCommand(ChatHandler* handler, char const* args)
    {
        if (!*args)
            return false;

        return HandleAddDisables(handler, args, DISABLE_TYPE_MMAP);
    }

    bool HandleRemoveDisables(ChatHandler* handler, char const* args, uint8 disableType)
    {
        char* entryStr = strtok((char*)args, " ");
        if (!entryStr || !atoi(entryStr))
            return false;

        uint32 entry = uint32(atoi(entryStr));

        std::string disableTypeStr = "";

        switch (disableType)
        {
        case DISABLE_TYPE_SPELL:
            disableTypeStr = "spell";
            break;
        case DISABLE_TYPE_QUEST:
            disableTypeStr = "quest";
            break;
        case DISABLE_TYPE_MAP:
            disableTypeStr = "map";
            break;
        case DISABLE_TYPE_BATTLEGROUND:
            disableTypeStr = "battleground";
            break;
        case DISABLE_TYPE_ACHIEVEMENT_CRITERIA:
            disableTypeStr = "achievement criteria";
            break;
        case DISABLE_TYPE_OUTDOORPVP:
            disableTypeStr = "outdoorpvp";
            break;
        case DISABLE_TYPE_VMAP:
            disableTypeStr = "vmap";
            break;
        case DISABLE_TYPE_MMAP:
            disableTypeStr = "mmap";
            break;
        }

        PreparedStatement* stmt = NULL;
        stmt = WorldDatabase.GetPreparedStatement(WORLD_SEL_DISABLES);
        stmt->setUInt32(0, entry);
        stmt->setUInt8(1, disableType);
        PreparedQueryResult result = WorldDatabase.Query(stmt);
        if (!result)
        {
            handler->PSendSysMessage("This %s (Id: %u) is not disabled.", disableTypeStr.c_str(), entry);
            handler->SetSentErrorMessage(true);
            return false;
        }

        stmt = WorldDatabase.GetPreparedStatement(WORLD_DEL_DISABLES);
        stmt->setUInt32(0, entry);
        stmt->setUInt8(1, disableType);
        WorldDatabase.Execute(stmt);

        handler->PSendSysMessage("Remove Disabled %s (Id: %u)", disableTypeStr.c_str(), entry);
        return true;
    }

    bool HandleRemoveDisableSpellCommand(ChatHandler* handler, char const* args)
    {
        if (!*args)
            return false;

        return HandleRemoveDisables(handler, args, DISABLE_TYPE_SPELL);
    }

    bool HandleRemoveDisableQuestCommand(ChatHandler* handler, char const* args)
    {
        if (!*args)
            return false;

        return HandleRemoveDisables(handler, args, DISABLE_TYPE_QUEST);
    }

    bool HandleRemoveDisableMapCommand(ChatHandler* handler, char const* args)
    {
        if (!*args)
            return false;

        return HandleRemoveDisables(handler, args, DISABLE_TYPE_MAP);
    }

    bool HandleRemoveDisableBattlegroundCommand(ChatHandler* handler, char const* args)
    {
        if (!*args)
            return false;

        return HandleRemoveDisables(handler, args, DISABLE_TYPE_BATTLEGROUND);
    }

    bool HandleRemoveDisableAchievementCriteriaCommand(ChatHandler* handler, char const* args)
    {
        if (!*args)
            return false;

        return HandleRemoveDisables(handler, args, DISABLE_TYPE_ACHIEVEMENT_CRITERIA);
    }

    bool HandleRemoveDisableOutdoorPvPCommand(ChatHandler* handler, char const* args)
    {
        if (!*args)
            return false;

        return HandleRemoveDisables(handler, args, DISABLE_TYPE_OUTDOORPVP);
    }

    bool HandleRemoveDisableVmapCommand(ChatHandler* handler, char const* args)
    {
        if (!*args)
            return false;

        return HandleRemoveDisables(handler, args, DISABLE_TYPE_VMAP);
    }

    bool HandleRemoveDisableMMapCommand(ChatHandler* handler, char const* args)
    {
        if (!*args)
            return false;

        return HandleRemoveDisables(handler, args, DISABLE_TYPE_MMAP);
    }

}}}

void AddSC_disable_commandscript()
{
    using namespace chat::command;
    using namespace chat::command::handler;

    new LegacyCommandScript("cmd_disable_add_spell",                    HandleAddDisableSpellCommand,                   true);
    new LegacyCommandScript("cmd_disable_add_quest",                    HandleAddDisableQuestCommand,                   true);
    new LegacyCommandScript("cmd_disable_add_map",                      HandleAddDisableMapCommand,                     true);
    new LegacyCommandScript("cmd_disable_add_battleground",             HandleAddDisableBattlegroundCommand,            true);
    new LegacyCommandScript("cmd_disable_add_achievement_criteria",     HandleAddDisableAchievementCriteriaCommand,     true);
    new LegacyCommandScript("cmd_disable_add_outdoorpvp",               HandleAddDisableOutdoorPvPCommand,              true);
    new LegacyCommandScript("cmd_disable_add_vmap",                     HandleAddDisableVmapCommand,                    true);
    new LegacyCommandScript("cmd_disable_add_mmap",                     HandleAddDisableMMapCommand,                    true);

    new LegacyCommandScript("cmd_disable_remove_spell",                 HandleRemoveDisableSpellCommand,                true);
    new LegacyCommandScript("cmd_disable_remove_quest",                 HandleRemoveDisableQuestCommand,                true);
    new LegacyCommandScript("cmd_disable_remove_map",                   HandleRemoveDisableMapCommand,                  true);
    new LegacyCommandScript("cmd_disable_remove_battleground",          HandleRemoveDisableBattlegroundCommand,         true);
    new LegacyCommandScript("cmd_disable_remove_achievement_criteria",  HandleRemoveDisableAchievementCriteriaCommand,  true);
    new LegacyCommandScript("cmd_disable_remove_outdoorpvp",            HandleRemoveDisableOutdoorPvPCommand,           true);
    new LegacyCommandScript("cmd_disable_remove_vmap",                  HandleRemoveDisableVmapCommand,                 true);
    new LegacyCommandScript("cmd_disable_remove_mmap",                  HandleRemoveDisableMMapCommand,                 true);
}